package com.gk.controller.api;
import com.eight.pojo.TbCoupon;
import com.eight.pojo.TbUser;
import com.eight.util.JWTTool;
import com.eight.util.WebUtil;
import com.eight.vo.ComResult;
import com.eight.vo.HeadPicVo;
import com.eight.vo.TbUserVo;
import com.gk.fein.SelectXCuponFindIdFein;
import com.gk.fein.UserEnableCouponFein;
import com.gk.service.TbUserService;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 客户操作控制层
 * &#064;author:  GK
 * &#064;date:  2023/2/27 10:33
 * &#064;version:No1.0
 */
@RestController
@RequestMapping("/customer/api/tbClient/")
public class TbUserApi {
    /**
     * 导入用户业务层
     */
    @Resource
    private TbUserService tbUserService;

    /**
     * 查询所有优惠卷接口
     */
    @Resource
    private UserEnableCouponFein userEnableCouponFein;

    /**
     * 查优惠卷的fein接口
     */
    @Resource
    private SelectXCuponFindIdFein selectXCuponFindIdFein;

    /**
     * 根据条件进行数据的查询和分页
     * @param pageIndex 当前页
     * @param pageSize  页中数据条数
     * @return 返回查询的数据
     */
    @PostMapping("selectAll/{pageIndex}/{pageSize}")
    public ComResult selectAll(@RequestBody TbUserVo tbUserVo, @PathVariable Integer pageIndex, @PathVariable Integer pageSize) {
        return tbUserService.selectAll(tbUserVo, pageIndex, pageSize);
    }

    /**
     * 根据用户id删除用户
     * @param id 用户id
     * @return 返回前端数据
     */
    @DeleteMapping("deleteFindId/{id}")
    public ComResult deleteFindId(@PathVariable Integer id) {
        return tbUserService.deleteFindId(id);
    }

    /**
     * 修改用户信息
     * @param tbUser 修改过的信息
     * @return 返回前端信息
     */
    @PostMapping("updateUser")
    public ComResult updateUser(@RequestBody TbUser tbUser) {
        return tbUserService.updateUser(tbUser);
    }

    /**
     * 添加用户信息
     * @param tbUser
     * @return
     */
    @PostMapping("insertUser")
    public ComResult insertUser(@RequestBody TbUser tbUser) {
        return tbUserService.insertUser(tbUser);
    }

    /**
     * 根据id查询客户信息
     *
     * @param id 查询条件
     * @return 响应前端
     */
    @PostMapping("selectFindId/{id}")
    public ComResult selectFindId(@PathVariable Integer id) {
        return tbUserService.selectFindId(id);
    }


    /**
     * 查询所有可用优惠卷
     * @return 响应前端
     */
    @GetMapping("selectAllCoupon")
    public ComResult selectCoupon() {
        List<TbCoupon> useEnableCoupon = userEnableCouponFein.getUseEnableCoupon();
        if (useEnableCoupon != null) {
            return new ComResult(2000, "查询成功", useEnableCoupon);
        }
        return new ComResult(5000, "查询失败");
    }

    /**
     * 根据ids查优惠卷
     * @param ids 查询条件
     * @return 响应前端
     */
    @PostMapping("selectXCuponFindId")
    public ComResult selectXCuponFindIdFein(@RequestBody List<Integer> ids) {
        return new ComResult(2000, "查询成功", selectXCuponFindIdFein.batchSearch(ids));
    }


    /**
     * 根据手机号和密码进行添加用户
     * @param username 用户账号
     * @param password 用户密码
     * @return 响应其前端
     */
    @PostMapping("insertOneUser/{username}/{password}")
    public ComResult insertOneUser(@PathVariable String username, @PathVariable String password) {
        return tbUserService.insertOneUser(username, password);
    }


    /**
     * 查询账号是否存在
     * @param phone 账号
     * @return 响应前端
     */
    @GetMapping("selectUsername/{phone}")
    public ComResult selectUsername(@PathVariable String phone) {
        System.out.println(phone);
        return tbUserService.selectUsername(phone);
    }


    /**
     * 根据账号和密码修改密码
     * @param username 账号
     * @param password 密码
     * @return 响应前端数据
     */
    @PostMapping("updateOneUser/{username}/{password}")
    public ComResult updateOneUser(@PathVariable String username,@PathVariable String password){
        return tbUserService.updateOneUser(username,password);
    }

    /**
     * 根据账号查询账户余额
     * @return 响应前端数据
     */
    @PostMapping("/selectMoney/")
    public ComResult selectMoney(HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> stringObjectMap = JWTTool.analyzeToken(token);
        String usernname = (String) stringObjectMap.get("username");
        return tbUserService.selectMoney(usernname);
    }

    /**
     * 充值添加充值额度
     * @param amount 金额
     * @return 响应前端数据
     */
    @PostMapping("addMoney/{amount}/{name}/{integration}")
    public ComResult addMoney(@PathVariable Integer amount,@PathVariable String name,@PathVariable Integer integration){
        return   tbUserService.addMoney(amount,name,integration);
    }
    /**
     * 根据用户账号查询用户信息
     * @param username 用户账号
     * @return 响应前端
     */
    @GetMapping("/selectOneTbUser/{username}")
    public TbUser selectTbUserOne(@PathVariable String username){
        return tbUserService.selectTbUser(username);
    }


    /**
     * 根据账号修改头像
     * @return 响应前端
     */
    @PostMapping("updateHeadPic")
    public ComResult updateHeadPic(@RequestBody HeadPicVo headPicVo){
        System.out.println(headPicVo.toString());
        return tbUserService.updateHeadPic(headPicVo.getUsername(), headPicVo.getHeadPic());
    }


    /**
     * 根据账号查询用户信息
     * @param username 账号
     * @return 放回查询信息
     */
    @GetMapping("getuserinfo/{username}")
    public ComResult getuserinfo(@PathVariable String username){
        return tbUserService.getuserinfo(username);
    }

    /**
     * 校验密码是否为原密码
     * @param username 账号
     * @param password 密码
     * @return 响应前端
     */
    @PostMapping("selectPassword/{username}/{password}")
    public ComResult selectPassword(@PathVariable String username, @PathVariable String password){
        return  tbUserService.selectPassword(username,password);
    }

    /**
     * 修改密码
     * @param username 账号
     * @param password 密码
     * @return 1
     */
    @PostMapping("updatePassword/{username}/{password}")
    public ComResult updatePassword(@PathVariable String username,@PathVariable String password){
        return tbUserService.updatePassword(username,password);
    }

    /**
     * 完善个人信息
     * @param tbUserVo 不填的个人信息
     * @return
     */
    @PostMapping("updateUserOver")
    public ComResult updateUserOver(@RequestBody TbUserVo tbUserVo){
        System.out.println(tbUserVo);
     return tbUserService.updateUserOver(tbUserVo);
    }


}
